import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCityStore = defineStore('city', () => {
  const savedCities = ref([])

  // 初始化（从 localStorage 读取）
  const loadSavedCities = () => {
    const stored = localStorage.getItem('savedCities')
    if (stored) {
      savedCities.value = JSON.parse(stored)
    }
  }

  // 添加城市
  const addCity = (city) => {
    if (!savedCities.value.includes(city)) {
      savedCities.value.push(city)
      localStorage.setItem('savedCities', JSON.stringify(savedCities.value))
    }
  }

  // 删除城市
  const removeCity = (city) => {
    savedCities.value = savedCities.value.filter((c) => c !== city)
    localStorage.setItem('savedCities', JSON.stringify(savedCities.value))
  }

  return {
    savedCities,
    addCity,
    removeCity,
    loadSavedCities,
  }
})